package com.pengpeng.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.User;

public class UserDao{
	

	private DatabaseFactory databasefactory;
	
	public User queryByUserphonenum(String userphonenum)
	{
		RowMapper<User> mapper=new RowMapper<User>()
		{
			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				String phonenum="";
				String name="";
				String ifonline="";
				String location="";
				String locatcontent="";
				String locattimestr="";
				Date locattime;
				
				phonenum=rs.getString("phonenum");
				name=rs.getString("name");
				ifonline=rs.getString("ifonline");
				location=rs.getString("location");
				locatcontent=rs.getString("locatcontent");
				locattimestr=rs.getString("locattime");
		
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					locattime= dateformat.parse(locattimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					locattime=new Date();
				}
				
				
				User user=new User();
				user.setPhonenum(phonenum);
				user.setName(name);
				user.setIfonline(ifonline);
				user.setLocation(location);
				user.setLocatcontent(locatcontent);
				user.setLocattime(locattime);
				
				return user;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryUserByphonenum");
		final Object[] params = new Object[]{userphonenum};
	    try {  
	    	  User user=jdbctemplate.queryForObject(sqlstr,params, mapper);
			  return  user;
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//queryByUserphonenum
	
	public List<User> queryByIfonline(String ifonline)
	{
		RowMapper<User> mapper=new RowMapper<User>()
		{
			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				String phonenum="";
				String name="";
				String ifonline="";
				String location="";
				String locatcontent="";
				String locattimestr="";
				Date locattime;
				
				phonenum=rs.getString("phonenum");
				name=rs.getString("name");
				ifonline=rs.getString("ifonline");
				location=rs.getString("location");
				locatcontent=rs.getString("locatcontent");
				locattimestr=rs.getString("locattime");
				
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					locattime= dateformat.parse(locattimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					locattime=new Date();
				}
								
				User user=new User();
				user.setPhonenum(phonenum);
				user.setName(name);
				user.setIfonline(ifonline);
				user.setLocation(location);
				user.setLocatcontent(locatcontent);
				user.setLocattime(locattime);
				
				return user;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryUsersByIfonline");
		final Object[] params = new Object[]{ifonline};
	    try {  
	    	  List<User> userlist=jdbctemplate.query(sqlstr,params,mapper);
			  return  userlist;
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//queryByIfonline
	
	public boolean insert(User user)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String locattime=dateformat.format(user.getLocattime());
			
		Object[] params = new Object[] {user.getPhonenum(),user.getName(),user.getIfonline(),user.getLocation(),user.getLocatcontent(),locattime};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertUser");
		jdbctemplate.update(sqlstr,params);
		
		return true;
	}//insert
	
	public boolean update(User user)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String locattime=dateformat.format(user.getLocattime());
		Object[] params = new Object[] {user.getPhonenum(),user.getName(),user.getIfonline(),user.getLocation(),user.getLocatcontent(),locattime,user.getPhonenum()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateUser");
		jdbctemplate.update(sqlstr,params);
		
		return true;
	}//update

	public boolean delete(User user)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {user.getPhonenum()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteUser");
		jdbctemplate.update(sqlstr,params);
		
		return true;
	}//delete

	public DatabaseFactory getDatabasefactory() {
		return databasefactory;
	}
    
	public void setDatabasefactory(DatabaseFactory databasefactory) {
		this.databasefactory = databasefactory;
	}
	
}
